local extension = Package:new("wq_daiduo")
extension.extensionName = "FkWqmt"
Fk:loadTranslationTable{
  ["wq_daiduo"] = "无期迷途·怠惰",
}

local Wqc = require "packages/FkWqmt/wq_common"

-- 禁闭者：卡门奈特
local wq_kamennaite = General:new(extension, "wq_kamennaite", "qun", 3, 3, General.Female)
-- 技能：魂碎
local wq_skill_hunsui = fk.CreateTriggerSkill{
  name = "wq_skill_hunsui",
  events = {fk.AfterCardsMove},
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if player:isAlive() and #Wqc.getMarkOrInitTable(player, "wq_skill_hunsui_mark") == 2 then
      local mark = Wqc.getMarkOrInitTable(player, "wq_skill_hunsui_mark")
      local x = mark[2]
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          x = x + #move.moveInfo
        end
      end
      local denominator = #room.alive_players < 5 and 4 or 8
      local new_move = x // denominator
      local new_mod = x % denominator
      mark[1] = mark[1] + new_move
      mark[2] = new_mod
      room:setPlayerMark(player, "wq_skill_hunsui_mark", mark)
      if new_move > 0 and player:hasSkill(self) then
        data.cost_data = new_move
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_hunsui_prompt")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = data.cost_data
    room:drawCards(player, num, self.name)
    room:addPlayerMark(player, "wq_skill_hunsui_invoked", 1)
  end,
  refresh_events = {fk.GameStart, fk.RoundStart, fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return player:hasSkill(self) and player == target and data == self
    elseif event == fk.GameStart or event == fk.RoundStart then
      return player:hasSkill(self)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      room:setPlayerMark(player, "wq_skill_hunsui_invoked", 0)
    elseif event == fk.GameStart or event == fk.EventAcquireSkill then
      room:setPlayerMark(player, "wq_skill_hunsui_mark", {0, 0})
    elseif event == fk.EventLoseSkill then
      room:setPlayerMark(player, "wq_skill_hunsui_mark", nil)
    end
  end,
}
-- 技能：口腹
local wq_skill_koufu = fk.CreateActiveSkill{
  name = "wq_skill_koufu",
  anim_type = "support",
  card_num = 0,
  target_num = 0,
  prompt = function (self, selected, selected_cards)
    local num = math.max(math.min(Self:getMark("wq_skill_hunsui_invoked"), 3), 1)
    return "#wq_skill_koufu_prompt:::"..num
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local num = math.max(math.min(player:getMark("wq_skill_hunsui_invoked"), 3), 1)
    room:drawCards(player, num, self.name)
    num = math.min(#player:getCardIds{Player.Hand, Player.Equip}, num)
    local targets, cards = room:askForChooseCardsAndPlayers(player, num, num, table.map(room:getAlivePlayers(), function (p) return p.id end), 1, 1, nil, "#wq_skill_koufu_prompt_choose:::"..num, self.name, false)
    if #targets > 0 and #cards > 0 then
      local target = room:getPlayerById(targets[1])
      room:moveCardTo(cards, Player.Hand, target, fk.ReasonGive, self.name, nil, false)
      Wqc.clearNagtiveMarks(target)
      target:reset()
      room:recover {
        who = target,
        num = 1,
        recoverBy = player,
        skillName = self.name
      }
    end
  end,
}
-- 技能：王座
local wq_skill_wangzuo = fk.CreateActiveSkill{
  name = "wq_skill_wangzuo",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  prompt = "#wq_skill_wangzuo_prompt",
  frequency = Skill.Limited,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id and #Fk:currentRoom():getPlayerById(to_select):getCardIds("e") > 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cid = room:askForCardChosen(player, target, "e", self.name)
    if cid then
      target:addToPile("wq_pile_shi", cid, true, self.name)
      if player:isAlive() then
        room:addPlayerMark(player, "wq_skill_hunsui_invoked", 2)
      end
    end
  end,
}
local wq_skill_wangzuo_buff = fk.CreateTriggerSkill{
  name = "#wq_skill_wangzuo_buff",
  refresh_events = {fk.DamageCaused},
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return data.to and #data.to:getPile("wq_pile_shi") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    data.damage = data.damage * 2
    local pile = data.to:getPile("wq_pile_shi")
    if #pile > 0 then
      room:moveCards({
        from = data.to.id,
        ids = pile,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonPutIntoDiscardPile,
        skillName = "wq_skill_wangzuo",
      })
    end
  end,
}
wq_skill_wangzuo:addRelatedSkill(wq_skill_wangzuo_buff)
-- 卡门奈特技能
wq_kamennaite:addSkill(wq_skill_hunsui)
wq_kamennaite:addSkill(wq_skill_koufu)
wq_kamennaite:addSkill(wq_skill_wangzuo)
-- 卡门奈特翻译
Fk:loadTranslationTable{
  ["wq_kamennaite"] = "卡门奈特",
  ["#wq_kamennaite"] = "灵魂萃取",
  ["designer:wq_kamennaite"] = "妙霄",
  ["cv:wq_kamennaite"] = "官方",
  ["illustrator:wq_kamennaite"] = "官方",
  ["wq_skill_hunsui"] = "魂碎",
  [":wq_skill_hunsui"] = "进入弃牌堆的牌每次达到“八”的倍数张时（存活人数小于5时改为“四”的倍数），你可以摸一张牌。",
  ["$wq_skill_hunsui"] = "我嗅到了灵魂的芬芳！",
  ["#wq_skill_hunsui_prompt"] = "魂碎：你可以摸一张牌",
  ["wq_skill_koufu"] = "口腹",
  [":wq_skill_koufu"] = "出牌阶段限一次，你可以摸X张牌，然后将等量的牌交给一名角色。若如此做，该角色移除其所有负面标记，重置其武将牌并回复1点体力（X为你本轮发动【魂碎】的次数，至少为1，至多为3）。"
  ..Wqc.getTranslateInNewLine("#wq_common_mark_negative_description"),
  ["$wq_skill_koufu"] = "让我来品鉴一二。",
  ["#wq_skill_koufu_prompt"] = "口腹：你可以摸 %arg 张牌，然后将等量的牌交给一名角色，该角色移除其所有负面标记，重置其武将牌并回复1点体力",
  ["#wq_skill_koufu_prompt_choose"] = "操作提示：选择 %arg 张牌 -> 选择一名角色",
  ["wq_skill_wangzuo"] = "王座",
  [":wq_skill_wangzuo"] = "限定技，出牌阶段，你可以将其他角色装备区里的一张牌置于其武将牌上，称为「实」。若如此做，你视为发动两次【魂碎】。武将牌上有「实」的角色受到伤害时，其需移去「实」并令伤害值翻倍。",
  ["$wq_skill_wangzuo1"] = "没有人能抵抗欢愉。",
  ["$wq_skill_wangzuo2"] = "只是这样的话，我可不知足。",
  ["#wq_skill_wangzuo_prompt"] = "王座：你可以将其他角色装备牌区里的一张牌置于其武将牌上称为「实」，然后视为发动两次【魂碎】<br>武将牌上有「实」的角色受到伤害时，其需移去「实」并令伤害值翻倍",
  ["wq_pile_shi"] = "实",
  ["~wq_kamennaite"] = "哈……哈哈哈……撕扯吧，吞咽吧……",
}

-- 禁闭者：瑟琳
local wq_selin = General:new(extension, "wq_selin", "qun", 3, 3, General.Female)
-- 技能：缄默
local wq_skill_jianmo = fk.CreateActiveSkill{
  name = "wq_skill_jianmo",
  anim_type = "offensive",
  card_num = 1,
  min_target_num = 1,
  max_target_num = 2,
  prompt = function (self, selected, selected_cards)
    return "#wq_skill_jianmo_prompt-choose"
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isNude()
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and (not Self:prohibitDiscard(Fk:getCardById(to_select)))
  end,
  target_filter = function(self, to_select, selected)
    return #selected < 2 and to_select ~= Self.id and (not Fk:currentRoom():getPlayerById(to_select):isNude())
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cid = effect.cards[1]
    room:throwCard(cid, self.name, player, player)
    local target_ids = {}
    if #effect.tos == 1 then
      local target = room:getPlayerById(effect.tos[1])
      local num = math.min(2, #target:getCardIds("he"))
      local cids = room:askForCardsChosen(player, target, 1, num, "he", self.name)
      room:throwCard(cids, self.name, target, player)
      if not player:isAlive() then return end
      if target:isAlive() then
        table.insert(target_ids, target.id)
      end
    else
      local target1 = room:getPlayerById(effect.tos[1])
      local target2 = room:getPlayerById(effect.tos[2])
      local card_id = room:askForCardsChosen(player, target1, 1, 1, "he", self.name)
      room:throwCard(card_id, self.name, target1, player)
      if not player:isAlive() then return end
      if target1:isAlive() then
        table.insert(target_ids, target1.id)
      end
      card_id = room:askForCardsChosen(player, target2, 1, 1, "he", self.name)
      room:throwCard(card_id, self.name, target2, player)
      if not player:isAlive() then return end
      if target2:isAlive() then
        table.insert(target_ids, target2.id)
      end
    end
    local tos = room:askForChoosePlayers(player, target_ids, 1, 1, "#wq_skill_jianmo_prompt-slash", self.name, true)
    if #tos > 0 then
      local dest = room:getPlayerById(tos[1])
      room:useVirtualCard("slash", nil, player, dest, self.name, true)
    end
  end,
}
-- 技能：折射
local wq_skill_zheshe = fk.CreateTriggerSkill{
  name = "wq_skill_zheshe",
  anim_type = "offensive",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.to and data.to:isAlive() and player ~= data.to and
    table.find(player.room.alive_players, function(p) return data.to:distanceTo(p) == 1 and p ~= player end) and
    player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room.alive_players, function(p) return data.to:distanceTo(p) == 1 and p ~= player end), Util.IdMapper)
    if #targets == 0 then return false end
    local tos = room:askForChoosePlayers(player, targets, 1, 1, "#wq_skill_zheshe_prompt::"..data.to.id, self.name, true)
    if #tos > 0 then
      self.cost_data = tos[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dest = room:getPlayerById(self.cost_data)
    room:damage{
      from = player,
      to = dest,
      damage = 1,
      skillName = self.name,
    }
  end
}
-- 瑟琳的技能
wq_selin:addSkill(wq_skill_jianmo)
wq_selin:addSkill(wq_skill_zheshe)
-- 瑟琳的翻译
Fk:loadTranslationTable{
  ["wq_selin"] = "瑟琳",
  ["#wq_selin"] = "▇▇▇▇",
  ["designer:wq_selin"] = "妙霄",
  ["cv:wq_selin"] = "官方",
  ["illustrator:wq_selin"] = "官方",
  ["wq_skill_jianmo"] = "缄默",
  [":wq_skill_jianmo"] = "出牌阶段限一次，你可以弃置一张牌，并弃置至多两名其他角色的共计两张牌，然后你可以视为对其中一名角色使用一张无视距离且不计次数的【杀】。",
  ["$wq_skill_jianmo"] = "太吵了，安静！",
  ["#wq_skill_jianmo_prompt-choose"] = "缄默：你可以弃置一张牌，并弃置至多两名其他角色的共计两张牌<br>然后你可以视为对其中一名角色使用一张无视距离且不计次数的【杀】",
  ["#wq_skill_jianmo_prompt-slash"] = "缄默：你可以视为对其中一名角色使用一张无视距离且不计次数的【杀】",
  ["wq_skill_zheshe"] = "折射",
  [":wq_skill_zheshe"] = "每回合限一次，当你对其他角色造成伤害后，你可以对该角色距离1的另一名其他角色造成1点伤害。",
  ["$wq_skill_zheshe"] = "好了，到此为止，你已经走得太远。",
  ["#wq_skill_zheshe_prompt"] = "折射：你可以对%dest距离1的另一名其他角色造成1点伤害",
  ["~wq_selin"] = "呵……我先走了……在那边等你……",
}

-- 禁闭者：雷温
local wq_leiwen = General:new(extension, "wq_leiwen", "qun", 3, 3, General.Female)
-- 技能：调频
local wq_skill_tiaopin = fk.CreateActiveSkill{
  name = "wq_skill_tiaopin",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#wq_skill_tiaopin_prompt",
  can_use = function(self, player)
    local num = 0
    table.forEach(Fk:currentRoom().alive_players, function(p)
      num = num + #p:getPile("wq_pile_guai")
    end)
    return num < 4
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    local room = Fk:currentRoom()
    return #selected == 0 and to_select ~= Self.id and (not room:getPlayerById(to_select):isNude()) and #room:getPlayerById(to_select):getPile("wq_pile_guai") == 0
  end,
  on_use = function(self, room, effect)
    local target = room:getPlayerById(effect.tos[1])
    local cid = room:askForCardChosen(target, target, "he", self.name)
    if cid then
      target:addToPile("wq_pile_guai", cid, true, self.name)
      if target:isAlive() then
        room:addPlayerMark(target, "@wq_mark_kongju", 1)
      end
    end
  end,
}
-- 技能：共鸣
local wq_skill_gongming = fk.CreateTriggerSkill{
  name = "wq_skill_gongming",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(self) and player.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if #table.filter(room:getOtherPlayers(player), function(p) return #p:getPile("wq_pile_guai") > 0 end) > 0 then
      return room:askForSkillInvoke(player, self.name, data, "#wq_skill_gongming_prompt-invoke")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not player:isAlive() then
        break
      end
      if p:isAlive() and #p:getPile("wq_pile_guai") > 0 then
        local pattern = ""
        local exist_black = false
        local exist_red = false
        table.forEach(p:getPile("wq_pile_guai"), function(cid)
          if Fk:getCardById(cid):getColorString() == "black" then
            exist_black = true
          elseif Fk:getCardById(cid):getColorString() == "red" then
            exist_red = true
          end
        end)
        if exist_black and exist_red then
          pattern = "spade,club,heart,diamond"
        elseif exist_black then
          pattern = "spade,club"
        elseif exist_red then
          pattern = "heart,diamond"
        end
        if p:isNude() or #room:askForDiscard(p, 1, 1, true, self.name, true, ".|.|^("..pattern..")|hand,equip", "#wq_skill_gongming_prompt-discard:"..player.id) == 0 then
          local cids = p:getPile("wq_pile_guai")
          local dummy = Fk:cloneCard("dilu")
          dummy:addSubcards(cids)
          room:obtainCard(player, dummy, true, fk.ReasonPrey, player.id)
        end
        if player:isAlive() and p:isAlive() and p:getMark("@wq_mark_kongju") > 0 then
          room:damage{
            to = p,
            from = player,
            damage = 1,
            damageType = fk.ThunderDamage,
            skillName = self.name,
          }
        end
      end
    end
  end,
}
-- 技能：静音
local wq_skill_jingyin = fk.CreateActiveSkill{
  name = "wq_skill_jingyin",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#wq_skill_jingyin_prompt",
  frequency = Skill.Limited,
  can_use = function(self, player)
    local num = 0
    table.forEach(Fk:currentRoom().alive_players, function(p)
      num = num + #p:getPile("wq_pile_guai")
    end)
    return num > 0 and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local target = room:getPlayerById(effect.tos[1])
    local cids = {}
    table.forEach(room:getAlivePlayers(), function(p)
      if #p:getPile("wq_pile_guai") > 0 then
        table.forEach(p:getPile("wq_pile_guai"), function(cid)
          table.insert(cids, cid)
        end)
      end
    end)
    local dummy = Fk:cloneCard("dilu")
    dummy:addSubcards(cids)
    target:addToPile("wq_pile_guai", dummy, true, self.name)
    if target:isAlive() then
      room:addPlayerMark(target, "@wq_mark_kongju", 1)
    end
    if target:isAlive() then
      if #cids < 4 then
        cids = {}
        table.forEach(room:getNCards(4 - #cids), function(cid)
          table.insert(cids, cid)
        end)
        dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(cids)
        target:addToPile("wq_pile_guai", dummy, true, self.name)
      end
    end
  end,
}
-- 雷温的技能
wq_leiwen:addSkill(wq_skill_tiaopin)
wq_leiwen:addSkill(wq_skill_gongming)
wq_leiwen:addSkill(wq_skill_jingyin)
-- 雷温的翻译
Fk:loadTranslationTable{
  ["wq_leiwen"] = "雷温",
  ["#wq_leiwen"] = "波频扰乱",
  ["designer:wq_leiwen"] = "妙霄",
  ["cv:wq_leiwen"] = "官方",
  ["illustrator:wq_leiwen"] = "官方",
  ["wq_skill_tiaopin"] = "调频",
  [":wq_skill_tiaopin"] = "出牌阶段，若场上「怪」的数量不足四张，你可以选择一名没有「怪」的其他角色，令其将一张牌置于武将牌上称为「怪」，然后其获得1枚「恐惧」标记。"
  ..Wqc.getTranslateInNewLine("#wq_common_mark_kongju_description"),
  ["$wq_skill_tiaopin"] = "感谢您的收听！",
  ["#wq_skill_tiaopin_prompt"] = "调频：你可以令一名没有「怪」的其他角色将一张牌置于武将牌上称为「怪」，然后其获得1枚「恐惧」标记",
  ["wq_pile_guai"] = "怪",
  ["wq_skill_gongming"] = "共鸣",
  [":wq_skill_gongming"] = "结束阶段，你可以令所有武将牌上有「怪」的其他角色依次选择一项：1.弃置一张与「怪」的颜色均不同的牌；2.令你获得其武将牌上的「怪」。然后若该角色有「恐惧」标记，你对其造成1点雷电伤害。",
  ["$wq_skill_gongming"] = "我会把你给予我的一切，构造成束缚你的囚笼。",
  ["#wq_skill_gongming_prompt-invoke"] = "共鸣：你可以令所有武将牌上有「怪」的其他角色依次选择一项：<br>1.弃置一张与「怪」的颜色均不同的牌<br>2.令你获得其武将牌上的「怪」<br>然后若该角色有「恐惧」标记，你对其造成1点雷电伤害",
  ["#wq_skill_gongming_prompt-discard"] = "共鸣：请弃置一张与你武将牌上「怪」的颜色均不同的牌，否则你令%src获得「怪」",
  ["wq_skill_jingyin"] = "静音",
  [":wq_skill_jingyin"] = "限定技，出牌阶段，你可以将场上的所有「怪」移至一名其他角色的武将牌上，并令其获得1枚「恐惧」标记。然后若「怪」的数量不足四张，你将牌堆顶的X张牌置于其武将牌上称为「怪」（X为「怪」的数量与4的差值）。",
  ["$wq_skill_jingyin1"] = "欢迎收听凌晨3点的问候，在无光之夜四处寻求的你，是想要对我说些什么吗？",
  ["$wq_skill_jingyin2"] = "人们总喜欢在我身上寻找他们想要的，得以安慰他们的价值。呵，多可悲啊……",
  ["#wq_skill_jingyin_prompt"] = "静音：你可以将场上的所有「怪」移至一名其他角色的武将牌上，并令其获得1枚「恐惧」标记<br>然后若「怪」的数量不足四张，你将牌堆顶的X张牌置于其武将牌上称为「怪」（X为「怪」的数量与4的差值）",
  ["~wq_leiwen"] = "不……不……别再说了……",
}

return extension